package com.example.yuejian.activity;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.Log;
import android.widget.EditText;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.blankj.utilcode.util.SPUtils;
import com.blankj.utilcode.util.StringUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.example.yuejian.R;
import com.example.yuejian.bean.IsCollection;
import com.example.yuejian.common.PictureSelectUtils;
import com.yanzhenjie.permission.AndPermission;
import com.yanzhenjie.permission.Permission;
import com.yanzhenjie.permission.PermissionListener;
import com.yanzhenjie.permission.Rationale;
import com.yanzhenjie.permission.RationaleListener;
import com.zhouyou.http.EasyHttp;
import com.zhouyou.http.body.ProgressResponseCallBack;
import com.zhouyou.http.callback.SimpleCallBack;
import com.zhouyou.http.exception.ApiException;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.util.Arrays;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class SettingActivity extends BaseActivity {

    private static final String TAG = "SettingActivity";
    @BindView(R.id.pet)
    EditText editText;
    Handler handler_activity = new mHandler();
    @BindView(R.id.nickname)
    EditText nickname;
    @BindView(R.id.name)
    EditText name;
    @BindView(R.id.phone)
    EditText phone;
    private Context context;
    private String uid;

    // 步骤1：自定义Handler子类（继承Handler类） & 复写handleMessage（）方法
    class mHandler extends Handler {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 0:

                    IsCollection isCollection = null ;

                    try {
                        isCollection =JSON.parseObject(msg.obj.toString(), IsCollection.class);
                    }catch (Exception e){
                        ToastUtils.showShort("获取数据失败");
                        return;
                    }

                    if ("0".equals(isCollection.getError())) {
                        ToastUtils.showShort("修改成功");
                    } else {
                        ToastUtils.showShort("修改失败");
                    }
                    break;
                case 1:
                    break;
                case 2:
                    break;
            }
        }
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_setting);
        super.onCreate(savedInstanceState);
        context = this;
        uid = SPUtils.getInstance().getInt("uid") + "";
    }

    @OnClick(R.id.password)
    void password() {
        if (StringUtils.isEmpty(editText.getText().toString())) {
            ToastUtils.showShort("密码不能为空");
            return;
        }
        EasyHttp.get("yuejian6/public/index.php/json/my/update_pwd").params("user_id", uid).params("password", editText.getText().toString())
                .execute(new SimpleCallBack<String>() {
                    @Override
                    public void onSuccess(String data) {

                        Message msg = Message.obtain(); // 实例化消息对象
                        msg.what = 0; // 消息标识
                        msg.obj = data; // 消息内容存放

                        handler_activity.sendMessage(msg);
                    }

                    @Override
                    public void onError(ApiException error) {
                        Log.d(TAG, "onFailed: " + error);
                    }
                });
    }



    @OnClick(R.id.other)
    void other() {

        if (phone.getText().toString().length() != 11) {
            ToastUtils.showShort("电话格式错误");
            return;
        }
        EasyHttp.get("yuejian6/public/index.php/json/my/update_base")
                .params("user_id", uid)
                .params("realname", name.getText().toString())
                .params("nickname", nickname.getText().toString())
                .params("tel", phone.getText().toString())
                .execute(new SimpleCallBack<String>() {
                    @Override
                    public void onSuccess(String data) {

                        Message msg = Message.obtain(); // 实例化消息对象
                        msg.what = 0; // 消息标识
                        msg.obj = data; // 消息内容存放

                        handler_activity.sendMessage(msg);
                    }

                    @Override
                    public void onError(ApiException error) {
                        Log.d(TAG, "onFailed: " + error);
                    }
                });
    }


}
